<?php

namespace app\command;

use app\model\Talent;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Log;
use Wechat\CommerceApp;

class TalentCommand extends Command
{
    protected function configure()
    {
        $this->setName('talent')
            ->setDescription('获取达人列表');
    }

    protected function execute(Input $input, Output $output)
    {
        Log::init([
            'type'        => 'file',
            'apart_level' => ['error'],
            'level'       => ['error'],
            'path'        => app()->getRootPath() . "runtime/log/talent/",
            'max_files'   => 5,
        ]);
        try {
            $next_key = '';
            do {
                [$talent_list, $next_key, $has_more] = CommerceApp::getInstance()->getBindTalentList($next_key);
                $next_key = $next_key ?? '';
                $has_more = $has_more ?? false;

                $ids = $save = [];
                foreach ($talent_list as $item) {
                    $ids[] = $item['talent_appid'];
                }
                $lists = Talent::where('talent_appid', 'in', $ids)->column('talent_appid, id');

                foreach ($talent_list as $item) {
                    $talent_appid = $item['talent_appid'];
                    if (!isset($lists[$talent_appid])) {
                        $save[] = [
                            'talent_appid'                   => $talent_appid,
                            'talent_nickname'                => $item['talent_nickname'],
                            'talent_head_img'                => $item['talent_head_img'],
                            'bind_time'                      => $item['bind_time'],
                            'related_finder_exportname_list' => !empty($item['related_finder_exportname_list']) ? json_encode($item['related_finder_exportname_list']) : [],
                            'related_mp_biz_appid_list'      => !empty($item['related_mp_biz_appid_list']) ? json_encode($item['related_mp_biz_appid_list']) : [],
                        ];
                    } else {
                        $save[] = [
                            'id'                             => $lists[$talent_appid],
                            'talent_nickname'                => $item['talent_nickname'],
                            'talent_head_img'                => $item['talent_head_img'],
                            'related_finder_exportname_list' => !empty($item['related_finder_exportname_list']) ? json_encode($item['related_finder_exportname_list']) : [],
                            'related_mp_biz_appid_list'      => !empty($item['related_mp_biz_appid_list']) ? json_encode($item['related_mp_biz_appid_list']) : [],
                        ];
                    }
                }
                if ($save) {
                    (new Talent())->saveAll($save);
                }
            } while ($has_more);
        } catch (\Exception $e) {
            $log = [
                'msg'   => '获取达人列表失败',
                'error' => $e->getMessage(),
                'file'  => $e->getFile(),
                'line'  => $e->getLine(),
            ];
            Log::error($log);
            exit;
        }

    }
}
